package com.my.hzb.user.ctrl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.framelib.utils.Page;
import com.my.hzb.common.bean.LoginUserBean;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.common.util.CommonUtil;
import com.my.hzb.common.util.JsonUtil;
import com.my.hzb.entiy.Address;
import com.my.hzb.entiy.UserInfo;
import com.my.hzb.user.service.AddressService;

@Controller
@RequestMapping("/ads")
public class AddressCtrl {

	private static final Logger log = LogManager.getLogger(AddressCtrl.class);
	
	@Autowired
	private AddressService addressService;
	
	@RequestMapping("/add")
	@ResponseBody
	public ResultBean add(HttpServletRequest request,Address ads){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			LoginUserBean login = CommonUtil.getLoginUserByCoo(request, "hzb");
			if(null!=login){
				ads.setUserName(login.getUserName());
				Address adss = addressService.getDefByUser(login.getUserName());
				if(null!=adss){
					ads.setStatus(1);
				}else{
					ads.setStatus(0);
				}
				addressService.add(ads);
			}
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
			result.setRespMsg("Ok");
			result.setReturnObj("main");
		}catch(Exception e){
			log.error("添加地址失败",e);
		}
		return result;
	}
	@RequestMapping("/addByName")
	@ResponseBody
	public ResultBean addByName(HttpServletRequest request,Address ads){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		if(null!=ads&&null!=ads.getUserName()){
			Address adss = addressService.getDefByUser(ads.getUserName());
			if(null!=adss){
				ads.setStatus(1);
			}else{
				ads.setStatus(0);
			}
			addressService.add(ads);
		}
		result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		result.setRespMsg("Ok");
		result.setReturnObj("main");
		return result;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public ResultBean list(Address ads){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		List<Address> list = addressService.find(ads,new Page());
		if(null!=list){
			result.setReturnList(new ArrayList<Object>(list));
			result.setReturn(ResultBean.RETURN_SUCCESS_CODE, "ok");
		}
		return result;
	}
	
	@RequestMapping("/getListByUserName")
	@ResponseBody
	public ResultBean getListByUserName(String userName){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			Address ads = new Address();
			ads.setUserName(userName);
			List<Address> list = addressService.find(ads,new Page());
			if(null!=list){
				result.setReturnStr("地址,手机,收件人,状态");
				result.setCookie("adsInfo,adsPhone,adsReve,status");
				result.setReturnList(new ArrayList<Object>(list));
				result.setReturn(ResultBean.RETURN_SUCCESS_CODE, "ok");
				return result;
			}
			result.setRespMsg("查询失败");
		}catch(Exception e){
			log.error("查询地址列表失败",e);
		}
		return result;
	}
	@RequestMapping("/getByUserName")
	@ResponseBody
	public ResultBean getByUserName(String userName){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		Address ads = addressService.getDefByUser(userName);
		if(null!=ads){
			result.setReturnObj(ads);
			result.setReturnStr(JsonUtil.g.toJson(ads));
			result.setReturn(ResultBean.RETURN_SUCCESS_CODE, "ok");
			return result;
		}
		result.setRespMsg("查询失败");
		return result;
	}
}
